define(['jquery', 'api', 'user', 'util'], function($, api, user, util) {
    var vm,
            controller = {
                init: function(id) {
                    if (!user.checkLogin())
                        return;
                    api.footerShow(0);//隐藏底部导航
                    api.setTitle(L('html_title_user_commission'));
                    api.headerShow(1);
					api.headerTitle(L('html_title_user_commission'));
                    
                    this.render();
                },
                render: function() {
                    var that = this;
                    $('#common').html('<div id="commission"></div>');
                    require(['text!pages/user/commission.html', 'Vue'], function(tpl, Vue) {
                        vm = new Vue({
                            el: '#commission',
                            data: {
                                show: true,
                                loading:false,
                                rows: [],
                                sumCount: '--',
                                sumCharge: '--',
                                sumShareCharge: '--',
                                startTime:(function(d){return d.format('yyyy-MM-dd')})(new Date(new Date().getTime()-86400*1000)),
	                    		endTime:(function(d){return d.format('yyyy-MM-dd')})(new Date()),
                                form: {
                                    username: '',
                                    startTime:'',
	                    			endTime:'',
	                    			level:'1'
                                },
                                showMore:false
                            },
                            filters: {
                                priceFormat: function(v, mindecimal) {
                                    if (isNaN(v)) {
                                        return v == '--' ? v : '';
                                    }
                                    return v ? util.priceFormat(mindecimal, v) : '0.00';
                                }
                            },
                            methods: {
                                doQuery: function(more) {
                                	if(more === undefined){
//						                this.ordersHistory=[];
					                    pagestart=0;
					                }
                                	
                                	if(this.form.startTime==''){
						            	this.form.startTime = this.startTime;
						            }
						            if(this.form.endTime==''){
										this.form.endTime = this.endTime;            	
						            }

                                    var sumData = {
	                                        username: this.form.username,
	                                        begindate: this.form.startTime,
	                                        returnresulttype: 49,
	                                        pagestart: 0,
	                                        count: 1000,
	                                        enddate: this.form.endTime,
	                                        level:this.form.level
	                                    }, 
	                                    // 列表数据
	                                    rowData = $.extend({}, sumData, {
	                                        returnresulttype: 50
	                                    });
	                                    
                                    this.loading=true;
                                    
                                    // 分成汇总
                                    api.commission(sumData, 
                                    	function(rs) {
	                                        vm.loading=false;
	                                        if (rs.retcode == '0' && rs.data) {
	                                            vm.sumCount = rs.data.sumcount;
	                                            vm.sumCharge = rs.data.sumcharge;
	                                            vm.sumShareCharge = rs.data.sumsharecharge;
	                                            vm.rows=[];
	                                            
	                                            //分成列表
	                                            if(!isNaN(vm.sumCount) && vm.sumCount>0){
				                                    //列表数据
				                                    vm.loading=true;
				                                    api.commission(rowData,
				                                    	function(rs){
				                                    		/*
				                                    		 {
															    "retcode": "0",
															    "data":
															    {
															        "sharecharge":
															        [
															            {
															                "useraccount": "4010011237019",
															                "username": "黄",
															                "sharecharge": "-37776",
															                "charge": "-188879"
															            }
															        ]
															    }
															}
				                                    		 * */
									                     	vm.loading=false;
									                         if(rs.retcode=='0' && rs.data){
									                            vm.rows=rs.data.sharecharge;
									                         }else{
									                         	vm.loading=false;
									                         }
									                     },
									                     function(){
									                     	vm.loading=false;
									                     }
				                                	);
			                                    }
	                                            
	                                        } else {
	                                            util.msg('暂无更多数据');
	                                            vm.sumCount = '-';
	                                            vm.sumCharge = '-';
	                                            vm.sumShareCharge = '-';
	                                            vm.rows=[];
	                                            vm.loading=false;
	                                        }
	                                    },
	                                    function(){
	                                    	vm.loading=false;
	                                    }
                                    );
                                    
                                    
                                }
                            },
                            template: tpl
                        })
                    })
                },
                onRouteChange: function() {
                    if (vm)
                        vm.show = false;
                }
            };
    return controller;
})